import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next';

import { LocalEnum, StorageEnum } from '@/types/enum';
import { getStringItem } from '@/utils/storage';

import en_US from './lang/en_US';
import zh_CN from './lang/zh_CN';
import app from './lang/zh_CN/app.json';
import chat from './lang/zh_CN/chat.json';
import common from './lang/zh_CN/common.json';
import dataset from './lang/zh_CN/dataset.json';
import file from './lang/zh_CN/file.json';
import login from './lang/zh_CN/login.json';
import publish from './lang/zh_CN/publish.json';
import user from './lang/zh_CN/user.json';
import workflow from './lang/zh_CN/workflow.json';

const defaultLng = getStringItem(StorageEnum.I18N) || (LocalEnum.zh_CN as string);

i18n
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: false,
    lng: defaultLng, // localstorage -> i18nextLng: en_US
    ns: [
      'common',
      'dataset',
      'app',
      'file',
      'publish',
      'workflow',
      'user',
      'chat',
      'login',
      'page',
      'translation',
    ],
    defaultNS: 'translation',
    fallbackLng: LocalEnum.zh_CN,
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
    resources: {
      en_US: { translation: en_US },
      zh_CN: {
        translation: zh_CN,
        app,
        chat,
        file,
        publish,
        common,
        user,
        workflow,
        dataset,
        login,
      },
    },
  });

export default i18n;
export const { t } = i18n;
